<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@page import="java.util.Map"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%@page import="com.pavco.caribbeanvisit.server.CountryServiceImpl"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.pavco.caribbeanvisit.shared.models.Country"%>
<%@page import="com.pavco.caribbeanvisit.shared.models.Tag"%>
<%@page import="com.pavco.caribbeanvisit.server.AttractionServiceImpl"%>
<%@page import="com.pavco.caribbeanvisit.shared.models.Attraction"%>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
	<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
	<title>Load data</title>	
</head>
<body>
<%
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	if (user != null) {
		if (user.getNickname().equalsIgnoreCase("pavelbyles")) {
		  %>
		  <div>
		  <form action="" method="get">
		  	<input type="hidden" name="load" value="true" />
		  	<input type="submit" value="load"/>
		  </form>
		  </div>
		  
		  <div>
		  <form action="" method="get">
			<label for="countrydelete">Delete Countries:</label>
		  	<input type="hidden" name="deletecountries" value="true" />
		  	<input type="submit" value="Go!" id="countrydelete"/>
		  </form>
		  </div>
		  
		  <div>
		  <form action="" method="get">
		  	<label for="attractiondelete">Delete Attractions:</label>
		  	<input type="hidden" name="deleteattractions" value="true" />
		  	<input type="submit" value="Go!" id="attractiondelete" />
		  </form>
		  </div>
		  <%
		}
	}
%>

<%
	// load and print countries and attractions
  if (user != null) {
  	if (user.getNickname().equalsIgnoreCase("pavelbyles")) {
			Map <String, Object> m = (Map <String, Object>)request.getParameterMap();
			if (m.containsKey("load")) {
			  String[] loadVals = (String[])m.get("load");
			  String loadValue = loadVals[0];
			  if (loadValue.equalsIgnoreCase("true")) {
			    CountryServiceImpl ci = new CountryServiceImpl();
			    ci.loadCountries();
			    %>
			    <div>Loaded countries in datastore</div>
			    <%
			    ArrayList<Country> countries = ci.getAllCountries();
			    if (countries.size() > 0) {
			      %>
			      <table border="1">
			      <tr>
			      <th>#</th><th>Country Name</th><th>Latitude</th><th>Longitude</th>
			      </tr>
			      <%
			    }
			    int i = 0;
			    for (Country country : countries) {
			      %>
			      <tr><td><%=++i %></td><td><%=country.getName() %></td><td><%=country.getLatitude() %></td><td><%=country.getLongitude() %></td></tr>
			      <%
			    }
			    if (countries.size() > 0) {
			      %></table><%
			    }
			    
			    AttractionServiceImpl ai = new AttractionServiceImpl();
			    ai.loadAttractions();
			    %>
			    <div>Loaded attractions in datastore</div>
			    <%
			    ArrayList<Attraction> attractions = ai.getAttractions();
			    if (attractions.size() > 0) {
			      %>
			      <table border="1">
			      <tr>
			      <th>#</th><th>Attraction.id</th><th>Country.id</th><th>Attraction Name</th><th>Latitude</th><th>Longitude</th><th>Tags</th>
			      </tr>
			      <%
			    }
			    i = 0;
			    for (Attraction attraction : attractions) {
			      %>
			      <tr><td><%=++i %></td>
			          <td><%=attraction.getEncodedKey() %></td>
			          <td><%=attraction.getCountryName() %></td>
			          <td><%=attraction.getName() %></td>
			          <td><%=attraction.getLatitude() %></td>
			          <td><%=attraction.getLongitude() %></td>
			          <td><% for (Tag tag : attraction.getTags()) {
				            		%><li><%=tag.getName() %></li><%
				          		 } %></td>
		          </tr>
			      <%
			    }
			    if (attractions.size() > 0) {
			      %></table><%
			    }
			  }
			} else if (m.containsKey("deletecountries")) {
			  String[] loadVals = (String[])m.get("deletecountries");
			  String loadValue = loadVals[0];
			  if (loadValue.equalsIgnoreCase("true")) {
			    CountryServiceImpl ci = new CountryServiceImpl();
			    ci.deleteAllCountries();
			    %><p>Deleted Countries</p><%
			  }
			} else if (m.containsKey("deleteattractions")) {
			  String[] loadVals = (String[])m.get("deleteattractions");
			  String loadValue = loadVals[0];
			  if (loadValue.equalsIgnoreCase("true")) {
			    AttractionServiceImpl ai = new AttractionServiceImpl();
			    ai.deleteAllAttractions();
			    %><p>Deleted Attractions</p><%
			  }
			} else if (m.containsKey("deletetags")) {
			  String[] loadVals = (String[])m.get("deletetags");
			  String loadValue = loadVals[0];
			  if (loadValue.equalsIgnoreCase("true")) {
			    
			  }
			}
  	}
  }
%>
</body>
</html>